from google.appengine.ext import webapp

from engine.Person import *

def performLogin(email, password):
    person = LoadPerson(email)
    if not person:
        return StatusReply("person not found")
    if person.password != password:
        return StatusReply("wrong password")
    return """
{
    "status": "ok",
    "userId": "%s"
}
""" % (person.id)

def getActivity(userId):
    if not userId or userId == "None" or userId == None:
        return StatusReply("no user id")
    person = LoadPersonFromId(userId)
    if not person:
        return StatusReply("person not found")
    date = TimeNow()
    person.setLocation(date)
    person.setActivity(date)
    return """
{
    "status": "ok",
    "time": "%s",
    "activity": "%s"
}
""" % (PrintTime(TimeNow()), person.currentActivity.name)

def performCheckIn(userId, position, activityId):
    if not userId or userId == "None" or userId == None:
        return StatusReply("no user id")
    if not activityId or activityId == "None" or activityId == None:
        return StatusReply("no activity id")
    person = LoadPersonFromId(userId)
    if not person:
        return StatusReply("person not found")
    location = Location()
    location.lat = float(position[0])
    location.lng = float(position[1])
    location.save()
    try:
        activity = LoadActivityFromId(activityId)
    except:
        return StatusReply("Unable to load activity with id %s" % activityId)
    
    person.addCheckIn(activity, TimeNow(), location)
    return StatusReply("ok")

def getCheckIns(userId, position):
    if not userId or userId == "None" or userId == None:
        return StatusReply("no user id")
    person = LoadPersonFromId(userId)
    if not person:
        return StatusReply("person not found")
    location = Location()
    location.lat = float(position[0])
    location.lng = float(position[1])
    activities = person.getActivities(location)
    if len(activities) == 0:
        return StatusReply("no activities found")
    checkIns = ""
    checkInIds = ""
    for a in activities:
        if checkIns != "":
            checkIns = checkIns + ","
            checkInIds = checkInIds + ","
        checkIns = checkIns + "\"" + a.name + "\""
        checkInIds = checkInIds + "\"" + str(a.id) + "\""
    return """
{
    "status": "ok",
    "checkIns": [ %s ],
    "checkInIds": [ %s ]
}
""" % (checkIns, checkInIds)

def StatusReply(status):
        return "{ \"status\": \"%s\" }" % (status)

class MobileCom(webapp.RequestHandler):
    def get(self):
        command = self.request.get('cmd', '')
        if not command or command == "None" or command == None:
            self.response.out.write("Error: no command")
            return

        if command == "login":
            email = self.request.get('email').strip()
            password = self.request.get('password').strip()
            self.response.out.write(performLogin(email, password))
            return

        if command == "checkIn":
            userId = self.request.get('userId', '')
            position = (self.request.get('lng', ''), self.request.get('lat', ''))
            activity = self.request.get('activity', '')
            self.response.out.write(performCheckIn(userId, position, activity))
            return

        if command == "getCheckIns":
            userId = self.request.get('userId', '')
            position = (self.request.get('lng', ''), self.request.get('lat', ''))
            self.response.out.write(getCheckIns(userId, position))
            return

        if command == "activity":
            userId = self.request.get('userId', '')
            self.response.out.write(getActivity(userId))
            return

        self.response.out.write(StatusReply("unknown command"))
        return
